/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using Photo;


/// <summary>
/// Summary description for LoginDAL
/// </summary>
namespace Photo
{

    public class LoginDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public LoginDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(LoginDO objLoginDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Username", SqlDbType.NVarChar);
            Sqlparam.Value = objLoginDO.Username;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Password", SqlDbType.NVarChar);
            Sqlparam.Value = objLoginDO.Password;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(LoginDO objLoginDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@UserID", SqlDbType.Int);
            Sqlparam.Value = objLoginDO.UserID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Username", SqlDbType.NVarChar);
            Sqlparam.Value = objLoginDO.Username;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Password", SqlDbType.NVarChar);
            Sqlparam.Value = objLoginDO.Password;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(LoginDO objLoginDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@UserID", SqlDbType.Int);
            Sqlparam.Value = objLoginDO.UserID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public LoginDO Select(LoginDO objLoginDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@UserID", SqlDbType.Int);
            Sqlparam.Value = objLoginDO.UserID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["UserID"]))
                    objLoginDO.UserID = Convert.ToInt32(dr["UserID"]);
                if (!Convert.IsDBNull(dr["Username"]))
                    objLoginDO.Username = Convert.ToString(dr["Username"]);
                if (!Convert.IsDBNull(dr["Password"]))
                    objLoginDO.Password = Convert.ToString(dr["Password"]);

            }
            return objLoginDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrLoginDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    LoginDO objLoginDO = new LoginDO();
                    if (!Convert.IsDBNull(dr["UserID"]))
                        objLoginDO.UserID = Convert.ToInt32(dr["UserID"]);
                    if (!Convert.IsDBNull(dr["Username"]))
                        objLoginDO.Username = Convert.ToString(dr["Username"]);
                    if (!Convert.IsDBNull(dr["Password"]))
                        objLoginDO.Password = Convert.ToString(dr["Password"]);
                    arrLoginDO.Add(objLoginDO);
                }
            }
            return arrLoginDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }


        #endregion


        public LoginDO SelectLogin(string username, string pass)
        {
            LoginDO objDO = new LoginDO();

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spLogin_Login";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@username", SqlDbType.NVarChar);
            Sqlparam.Value = username;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@pass", SqlDbType.NVarChar);
            Sqlparam.Value = pass;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["UserID"]))
                    objDO.UserID = Convert.ToInt32(dr["UserID"]);
                if (!Convert.IsDBNull(dr["Username"]))
                    objDO.Username = Convert.ToString(dr["Username"]);
                if (!Convert.IsDBNull(dr["Password"]))
                    objDO.Password = Convert.ToString(dr["Password"]);
                



            }
            return objDO;
        }
    }

}
